﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;
using Ortodoctor.Componentes;

namespace Ortodoctor.modelo
{
    class modeloDentista
    {
        //string connectionstring = ("Server=186.202.152.24;Port=5432;User Id=ortodoctor1;Password=DycbqDfHLI;Database=ortodoctor1;MinPoolSize=5;CommandTimeout=60;");
        string connectionstring = ("Server=localhost;Port=5432;User Id=postgres;Password=postgres;Database=ortodoctor;MinPoolSize=5;CommandTimeout=60;"); NpgsqlCommand com;
        NpgsqlConnection conn;
        NpgsqlDataAdapter da;
        //conexao con = new conexao();
        public modeloDentista()
        {
            conn = new NpgsqlConnection(connectionstring);
            conn.Open();
        }

        public void insert(Dentista d)
        {
            try
            {
                //con.open();
                String sql = "INSERT INTO dentista(id_clinica, id_funcionario) VALUES (:p1, :p2)";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("p1", d.Clinica.IdClinica);
                com.Parameters.AddWithValue("p2", d.Funcionario.IdFuncionario);
                com.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }

        public DataTable getAll()
        {
            DataTable dt = new DataTable();
            try
            {
                //con.open();
                //String sql = "SELECT * FROM dentista INNER JOIN clinica USING (id_clinica) " +
                //             "INNER JOIN funcionario using (id_funcionario)";
                String sql = "SELECT * FROM funcionario INNER JOIN clinica USING (id_clinica) ";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
                da.Fill(dt);
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }

        public DataTable getAll(int id_clinica)
        {
            DataTable dt = new DataTable();
            try
            {
                //con.open();
                String sql = "SELECT * FROM funcionario INNER JOIN clinica USING (id_clinica) where funcionario.id_clinica = " + id_clinica;
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
                da.Fill(dt);
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }

        public DataTable getDentista(int id)
        {
            DataTable dt = new DataTable();
            try
            {
                //con.open();
                String sql = "SELECT * FROM dentista INNER JOIN clinica USING (id_clinica) " +
                         "INNER JOIN funcionario using (id_funcionario) WHERE " +
                         "id_dentista=:id";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("id", id);
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(com);
                da.Fill(dt);
            }
            finally
            {
                conn.Close();
            }
            return dt;
        }

        public void update(Dentista d)
        {
            try
            {
                //con.open();
                String sql = "UPDATE dentista SET id_clinica=:p1, id_funcionario=:p2";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("p1", d.Clinica.IdClinica);
                com.Parameters.AddWithValue("p2", d.Funcionario.IdFuncionario);
                com.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }

        public void delete(int id)
        {
            try
            {
                //con.open();
                String sql = "DELETE FROM dentista WHERE id_dentista=:id";
                NpgsqlCommand com = new NpgsqlCommand(sql, conn);
                com.Parameters.AddWithValue("id", id);
                com.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
